
"use strict";
const Service = require("egg").Service;
const axios = require("axios");

const { Op } = require("sequelize");

const { failRes, successRes } = require("../extend/helper");
const { Journal } = require("../exception/Journal");
const await = require("await-stream-ready/lib/await");
const sUploadAvatar = require("./s_uploadAvatar");

function toInt(str) {
  if (typeof str === "number") return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class sCarousels extends Service {
  //轮播图查询
  async pc_s_query_carousels(params) {
    console.log("paramsUser:===============> ", params);
    let response = null;
    const { ctx } = this;
    try {
      const currentPage = toInt(params?.currentPage ?? 1);
      const pageSize = toInt(params?.pageSize ?? 10);

      let where = {
        title: {
          [Op.like]: `%${params?.title ?? ""}%`, // 使用 Op.like 实现模糊匹配
        },
        status: {
          [Op.like]: `%${params?.status ?? ""}%`, // 使用 Op.like 实现模糊匹配
        },
      };

      const result = await ctx.model.Carousels.findAndCountAll({
        where,
        model: ctx.model.Carousels,
        attributes: ["id", "title", "image", "status", "createdAt", "updatedAt"],

        order: [["updatedAt", "DESC"]], // 排序

        limit: pageSize, // 每页多少条
        offset: pageSize * (currentPage - 1), // 跳过多少条
      });

      await Journal(ctx, { operatorType: "轮播图信息 -> 查询", code: 200 });
      response = successRes({
        records: result?.rows ?? [],
        total: result?.count || 0,
        pageSize,
        currentPage,
      });
    } catch (error) {
      console.error(error, "<---- error - 轮播图信息查询");
      await Journal(ctx, { operatorType: "轮播图信息 -> 查询", code: 401 });
      response = failRes(401);
    }
    return response;
  }

  //轮播图新增
  async pc_s_create_carousels(params) {
    let response = null;
    const { ctx } = this;
    let transaction;
    try {
      // 建立事务对象
      transaction = await ctx.model.transaction();
      await ctx.model.Carousels.create(
        params,
        {
          attributes: {
            extends: ["id", "title", "image", "status"],
          },
        },
        { transaction }
        // 向数据库插入特定字段
      );

      await transaction.commit();
      await Journal(ctx, { operatorType: "轮播图信息 -> 新增", code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, "<---- error - 轮播图信息新增");
      await transaction.rollback();
      await Journal(ctx, { operatorType: "轮播图信息 -> 新增", code: 401 });
      response = failRes(401);
    }
    return response;
  }

  //轮播图修改
  async pc_s_update_carousels(params) {
    console.log(params, "<= params");
    let response = null;
    let transaction;
    try {
      const { ctx } = this;
      // 建立事务对象
      transaction = await ctx.model.transaction();
      await ctx.model.Carousels.update(
        params,
        {
          where: { id: params.id },
          attributes: { extends: ["id"] },
        },
        { transaction }

      );
      await transaction.commit();
      await Journal(ctx, { operatorType: "轮播图修改 -> 编辑", code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, "<---- error - 轮播图修改");
      await Journal(ctx, { operatorType: "轮播图修改 -> 编辑", code: 401 });
      response = failRes(401);
    }
    return response;
  }

  // 轮播图删除
  async pc_s_delete_carousels(params) {
    console.log(params, "<======= params");
    let response = null;
    const { ctx } = this;
    try {
      await ctx.model.Carousels.destroy({
        where: { id: params.id },
      });
      await Journal(ctx, { operatorType: "轮播图信息 -> 删除", code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, "<---- error - 轮播图删除");
      await Journal(ctx, { operatorType: "轮播图信息 -> 删除", code: 401 });
      response = failRes(401);
    }
    return response;
  }

  // 小程序端【获取轮播图数据】
  async wa_s_queryCarousels(params) {
    let response = null;
    const { ctx } = this;
    try {
      const reslut = await ctx.model.Carousels.findAll({
        where: { status: 1 },
        order: [["updatedAt", "DESC"]]
      });
      await Journal(ctx, { operatorType: "轮播图信息 -> wa查询", code: 200 });
      response = successRes(reslut);
    } catch (error) {
      console.error(error, "<---- error - 轮播图信息");
      await Journal(ctx, { operatorType: "轮播图信息 -> wa查询", code: 401 });
      response = failRes(401);
    }
    return response;
  }
}
module.exports = sCarousels;
